/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.cs.stats.ActivityTracker;
/*     */ import com.zimbra.cs.stats.ZimbraPerf;
/*     */ import java.io.InputStream;
/*     */ import java.io.Reader;
/*     */ import java.math.BigDecimal;
/*     */ import java.net.URL;
/*     */ import java.sql.Array;
/*     */ import java.sql.Blob;
/*     */ import java.sql.Clob;
/*     */ import java.sql.Connection;
/*     */ import java.sql.Date;
/*     */ import java.sql.NClob;
/*     */ import java.sql.ParameterMetaData;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.Ref;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.ResultSetMetaData;
/*     */ import java.sql.RowId;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.SQLWarning;
/*     */ import java.sql.SQLXML;
/*     */ import java.sql.Time;
/*     */ import java.sql.Timestamp;
/*     */ import java.util.Calendar;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class StatTrackingPreparedStatement
/*     */   implements PreparedStatement
/*     */ {
/*     */   private final PreparedStatement stmt;
/*     */   private String queryString;
/*  59 */   private ActivityTracker tracker = ZimbraPerf.SQL_TRACKER;
/*     */   
/*     */   public StatTrackingPreparedStatement(PreparedStatement stmt) {
/*  62 */     this.stmt = stmt;
/*  63 */     ZimbraPerf.incrementPrepareCount();
/*     */   }
/*     */   
/*     */   public StatTrackingPreparedStatement(PreparedStatement prepareStatement, String sql) {
/*  67 */     this(prepareStatement);
/*  68 */     setQueryString(sql);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setTracker(ActivityTracker tracker)
/*     */   {
/*  75 */     this.tracker = tracker;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setQueryString(String sql)
/*     */   {
/*  83 */     this.queryString = sql;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String getQueryType(String sql)
/*     */   {
/*  90 */     sql = sql.toLowerCase();
/*  91 */     if (sql.startsWith("select")) {
/*  92 */       return "SELECT";
/*     */     }
/*  94 */     if (sql.startsWith("insert")) {
/*  95 */       return "INSERT";
/*     */     }
/*  97 */     if (sql.startsWith("update")) {
/*  98 */       return "UPDATE";
/*     */     }
/* 100 */     if (sql.startsWith("delete")) {
/* 101 */       return "DELETE";
/*     */     }
/*     */     
/* 104 */     return "other";
/*     */   }
/*     */   
/*     */   private void trackQueryStats(String sql, long startTime)
/*     */   {
/* 109 */     if (sql != null) {
/* 110 */       this.tracker.addStat(getQueryType(sql), startTime);
/*     */     }
/*     */   }
/*     */   
/*     */   public ResultSet executeQuery(String sql) throws SQLException {
/* 115 */     long startTime = System.currentTimeMillis();
/* 116 */     ResultSet results = this.stmt.executeQuery(sql);
/* 117 */     trackQueryStats(sql, startTime);
/* 118 */     return results;
/*     */   }
/*     */   
/*     */   public int executeUpdate(String sql) throws SQLException
/*     */   {
/* 123 */     long startTime = System.currentTimeMillis();
/* 124 */     int results = this.stmt.executeUpdate(sql);
/* 125 */     trackQueryStats(sql, startTime);
/* 126 */     return results;
/*     */   }
/*     */   
/*     */   public void close() throws SQLException
/*     */   {
/* 131 */     this.stmt.close();
/*     */   }
/*     */   
/*     */   public int getMaxFieldSize() throws SQLException
/*     */   {
/* 136 */     return this.stmt.getMaxFieldSize();
/*     */   }
/*     */   
/*     */   public void setMaxFieldSize(int max) throws SQLException
/*     */   {
/* 141 */     this.stmt.setMaxFieldSize(max);
/*     */   }
/*     */   
/*     */   public int getMaxRows() throws SQLException
/*     */   {
/* 146 */     return this.stmt.getMaxRows();
/*     */   }
/*     */   
/*     */   public void setMaxRows(int max) throws SQLException
/*     */   {
/* 151 */     this.stmt.setMaxRows(max);
/*     */   }
/*     */   
/*     */   public void setEscapeProcessing(boolean enable) throws SQLException
/*     */   {
/* 156 */     this.stmt.setEscapeProcessing(enable);
/*     */   }
/*     */   
/*     */   public int getQueryTimeout() throws SQLException
/*     */   {
/* 161 */     return this.stmt.getQueryTimeout();
/*     */   }
/*     */   
/*     */   public void setQueryTimeout(int seconds) throws SQLException
/*     */   {
/* 166 */     this.stmt.setQueryTimeout(seconds);
/*     */   }
/*     */   
/*     */   public void cancel() throws SQLException
/*     */   {
/* 171 */     this.stmt.cancel();
/*     */   }
/*     */   
/*     */   public SQLWarning getWarnings()
/*     */     throws SQLException
/*     */   {
/* 177 */     return this.stmt.getWarnings();
/*     */   }
/*     */   
/*     */   public void clearWarnings()
/*     */     throws SQLException
/*     */   {
/* 183 */     this.stmt.clearWarnings();
/*     */   }
/*     */   
/*     */   public void setCursorName(String name) throws SQLException
/*     */   {
/* 188 */     this.stmt.setCursorName(name);
/*     */   }
/*     */   
/*     */   public boolean execute(String sql) throws SQLException
/*     */   {
/* 193 */     long startTime = System.currentTimeMillis();
/* 194 */     boolean results = this.stmt.execute(sql);
/* 195 */     trackQueryStats(sql, startTime);
/* 196 */     return results;
/*     */   }
/*     */   
/*     */   public ResultSet getResultSet() throws SQLException
/*     */   {
/* 201 */     return this.stmt.getResultSet();
/*     */   }
/*     */   
/*     */   public int getUpdateCount() throws SQLException
/*     */   {
/* 206 */     return this.stmt.getUpdateCount();
/*     */   }
/*     */   
/*     */   public boolean getMoreResults() throws SQLException
/*     */   {
/* 211 */     return this.stmt.getMoreResults();
/*     */   }
/*     */   
/*     */   public void setFetchDirection(int direction) throws SQLException
/*     */   {
/* 216 */     this.stmt.setFetchDirection(direction);
/*     */   }
/*     */   
/*     */   public int getFetchDirection() throws SQLException
/*     */   {
/* 221 */     return this.stmt.getFetchDirection();
/*     */   }
/*     */   
/*     */   public void setFetchSize(int rows) throws SQLException
/*     */   {
/* 226 */     this.stmt.setFetchSize(rows);
/*     */   }
/*     */   
/*     */   public int getFetchSize() throws SQLException
/*     */   {
/* 231 */     return this.stmt.getFetchSize();
/*     */   }
/*     */   
/*     */   public int getResultSetConcurrency() throws SQLException
/*     */   {
/* 236 */     return this.stmt.getResultSetConcurrency();
/*     */   }
/*     */   
/*     */   public int getResultSetType() throws SQLException
/*     */   {
/* 241 */     return this.stmt.getResultSetType();
/*     */   }
/*     */   
/*     */   public void addBatch(String sql) throws SQLException
/*     */   {
/* 246 */     this.stmt.addBatch(sql);
/*     */   }
/*     */   
/*     */   public void clearBatch() throws SQLException
/*     */   {
/* 251 */     this.stmt.clearBatch();
/*     */   }
/*     */   
/*     */   public int[] executeBatch() throws SQLException
/*     */   {
/* 256 */     return this.stmt.executeBatch();
/*     */   }
/*     */   
/*     */   public Connection getConnection() throws SQLException
/*     */   {
/* 261 */     return this.stmt.getConnection();
/*     */   }
/*     */   
/*     */   public boolean getMoreResults(int current) throws SQLException
/*     */   {
/* 266 */     return this.stmt.getMoreResults(current);
/*     */   }
/*     */   
/*     */   public ResultSet getGeneratedKeys() throws SQLException
/*     */   {
/* 271 */     return this.stmt.getGeneratedKeys();
/*     */   }
/*     */   
/*     */   public int executeUpdate(String sql, int autoGeneratedKeys)
/*     */     throws SQLException
/*     */   {
/* 277 */     return this.stmt.executeUpdate(sql, autoGeneratedKeys);
/*     */   }
/*     */   
/*     */   public int executeUpdate(String sql, int[] columnIndexes)
/*     */     throws SQLException
/*     */   {
/* 283 */     return this.stmt.executeUpdate(sql, columnIndexes);
/*     */   }
/*     */   
/*     */   public int executeUpdate(String sql, String[] columnNames)
/*     */     throws SQLException
/*     */   {
/* 289 */     return this.stmt.executeUpdate(sql, columnNames);
/*     */   }
/*     */   
/*     */   public boolean execute(String sql, int autoGeneratedKeys)
/*     */     throws SQLException
/*     */   {
/* 295 */     return this.stmt.execute(sql, autoGeneratedKeys);
/*     */   }
/*     */   
/*     */   public boolean execute(String sql, int[] columnIndexes) throws SQLException
/*     */   {
/* 300 */     return this.stmt.execute(sql, columnIndexes);
/*     */   }
/*     */   
/*     */   public boolean execute(String sql, String[] columnNames)
/*     */     throws SQLException
/*     */   {
/* 306 */     return this.stmt.execute(sql, columnNames);
/*     */   }
/*     */   
/*     */   public int getResultSetHoldability() throws SQLException
/*     */   {
/* 311 */     return this.stmt.getResultSetHoldability();
/*     */   }
/*     */   
/*     */   public boolean isClosed() throws SQLException
/*     */   {
/* 316 */     return this.stmt.isClosed();
/*     */   }
/*     */   
/*     */   public void setPoolable(boolean poolable) throws SQLException
/*     */   {
/* 321 */     this.stmt.setPoolable(poolable);
/*     */   }
/*     */   
/*     */   public boolean isPoolable() throws SQLException
/*     */   {
/* 326 */     return this.stmt.isPoolable();
/*     */   }
/*     */   
/*     */   public void closeOnCompletion() throws SQLException
/*     */   {
/* 331 */     this.stmt.closeOnCompletion();
/*     */   }
/*     */   
/*     */   public boolean isCloseOnCompletion() throws SQLException
/*     */   {
/* 336 */     return this.stmt.isCloseOnCompletion();
/*     */   }
/*     */   
/*     */   public <T> T unwrap(Class<T> iface) throws SQLException
/*     */   {
/* 341 */     return (T)this.stmt.unwrap(iface);
/*     */   }
/*     */   
/*     */   public boolean isWrapperFor(Class<?> iface) throws SQLException
/*     */   {
/* 346 */     return this.stmt.isWrapperFor(iface);
/*     */   }
/*     */   
/*     */   public ResultSet executeQuery() throws SQLException
/*     */   {
/* 351 */     long startTime = System.currentTimeMillis();
/* 352 */     ResultSet results = this.stmt.executeQuery();
/* 353 */     trackQueryStats(this.queryString, startTime);
/* 354 */     return results;
/*     */   }
/*     */   
/*     */   public int executeUpdate() throws SQLException
/*     */   {
/* 359 */     long startTime = System.currentTimeMillis();
/* 360 */     int results = this.stmt.executeUpdate();
/* 361 */     trackQueryStats(this.queryString, startTime);
/* 362 */     return results;
/*     */   }
/*     */   
/*     */   public void setNull(int parameterIndex, int sqlType) throws SQLException
/*     */   {
/* 367 */     this.stmt.setNull(parameterIndex, sqlType);
/*     */   }
/*     */   
/*     */   public void setBoolean(int parameterIndex, boolean x) throws SQLException
/*     */   {
/* 372 */     this.stmt.setBoolean(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setByte(int parameterIndex, byte x) throws SQLException
/*     */   {
/* 377 */     this.stmt.setByte(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setShort(int parameterIndex, short x) throws SQLException
/*     */   {
/* 382 */     this.stmt.setShort(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setInt(int parameterIndex, int x) throws SQLException
/*     */   {
/* 387 */     this.stmt.setInt(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setLong(int parameterIndex, long x) throws SQLException
/*     */   {
/* 392 */     this.stmt.setLong(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setFloat(int parameterIndex, float x) throws SQLException
/*     */   {
/* 397 */     this.stmt.setFloat(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setDouble(int parameterIndex, double x) throws SQLException
/*     */   {
/* 402 */     this.stmt.setDouble(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setBigDecimal(int parameterIndex, BigDecimal x)
/*     */     throws SQLException
/*     */   {
/* 408 */     this.stmt.setBigDecimal(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setString(int parameterIndex, String x) throws SQLException
/*     */   {
/* 413 */     this.stmt.setString(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setBytes(int parameterIndex, byte[] x) throws SQLException
/*     */   {
/* 418 */     this.stmt.setBytes(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setDate(int parameterIndex, Date x) throws SQLException
/*     */   {
/* 423 */     this.stmt.setDate(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setTime(int parameterIndex, Time x) throws SQLException
/*     */   {
/* 428 */     this.stmt.setTime(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setTimestamp(int parameterIndex, Timestamp x)
/*     */     throws SQLException
/*     */   {
/* 434 */     this.stmt.setTimestamp(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setAsciiStream(int parameterIndex, InputStream x, int length)
/*     */     throws SQLException
/*     */   {
/* 440 */     this.stmt.setAsciiStream(parameterIndex, x, length);
/*     */   }
/*     */   
/*     */ 
/*     */   public void setUnicodeStream(int parameterIndex, InputStream x, int length)
/*     */     throws SQLException
/*     */   {
/* 447 */     this.stmt.setUnicodeStream(parameterIndex, x, length);
/*     */   }
/*     */   
/*     */   public void setBinaryStream(int parameterIndex, InputStream x, int length)
/*     */     throws SQLException
/*     */   {
/* 453 */     this.stmt.setBinaryStream(parameterIndex, x, length);
/*     */   }
/*     */   
/*     */   public void clearParameters() throws SQLException
/*     */   {
/* 458 */     this.stmt.clearParameters();
/*     */   }
/*     */   
/*     */   public void setObject(int parameterIndex, Object x, int targetSqlType)
/*     */     throws SQLException
/*     */   {
/* 464 */     this.stmt.setObject(parameterIndex, x, targetSqlType);
/*     */   }
/*     */   
/*     */   public void setObject(int parameterIndex, Object x) throws SQLException
/*     */   {
/* 469 */     this.stmt.setObject(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public boolean execute() throws SQLException
/*     */   {
/* 474 */     long startTime = System.currentTimeMillis();
/* 475 */     boolean results = this.stmt.execute();
/* 476 */     trackQueryStats(this.queryString, startTime);
/* 477 */     return results;
/*     */   }
/*     */   
/*     */   public void addBatch() throws SQLException
/*     */   {
/* 482 */     this.stmt.addBatch();
/*     */   }
/*     */   
/*     */   public void setCharacterStream(int parameterIndex, Reader reader, int length)
/*     */     throws SQLException
/*     */   {
/* 488 */     this.stmt.setCharacterStream(parameterIndex, reader, length);
/*     */   }
/*     */   
/*     */   public void setRef(int parameterIndex, Ref x) throws SQLException
/*     */   {
/* 493 */     this.stmt.setRef(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setBlob(int parameterIndex, Blob x) throws SQLException
/*     */   {
/* 498 */     this.stmt.setBlob(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setClob(int parameterIndex, Clob x) throws SQLException
/*     */   {
/* 503 */     this.stmt.setClob(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setArray(int parameterIndex, Array x) throws SQLException
/*     */   {
/* 508 */     this.stmt.setArray(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public ResultSetMetaData getMetaData() throws SQLException
/*     */   {
/* 513 */     return this.stmt.getMetaData();
/*     */   }
/*     */   
/*     */   public void setDate(int parameterIndex, Date x, Calendar cal)
/*     */     throws SQLException
/*     */   {
/* 519 */     this.stmt.setDate(parameterIndex, x, cal);
/*     */   }
/*     */   
/*     */   public void setTime(int parameterIndex, Time x, Calendar cal)
/*     */     throws SQLException
/*     */   {
/* 525 */     this.stmt.setTime(parameterIndex, x, cal);
/*     */   }
/*     */   
/*     */   public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal)
/*     */     throws SQLException
/*     */   {
/* 531 */     this.stmt.setTimestamp(parameterIndex, x, cal);
/*     */   }
/*     */   
/*     */   public void setNull(int parameterIndex, int sqlType, String typeName)
/*     */     throws SQLException
/*     */   {
/* 537 */     this.stmt.setNull(parameterIndex, sqlType, typeName);
/*     */   }
/*     */   
/*     */   public void setURL(int parameterIndex, URL x) throws SQLException
/*     */   {
/* 542 */     this.stmt.setURL(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public ParameterMetaData getParameterMetaData() throws SQLException
/*     */   {
/* 547 */     return this.stmt.getParameterMetaData();
/*     */   }
/*     */   
/*     */   public void setRowId(int parameterIndex, RowId x) throws SQLException
/*     */   {
/* 552 */     this.stmt.setRowId(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setNString(int parameterIndex, String value)
/*     */     throws SQLException
/*     */   {
/* 558 */     this.stmt.setNString(parameterIndex, value);
/*     */   }
/*     */   
/*     */   public void setNCharacterStream(int parameterIndex, Reader value, long length)
/*     */     throws SQLException
/*     */   {
/* 564 */     this.stmt.setNCharacterStream(parameterIndex, value, length);
/*     */   }
/*     */   
/*     */   public void setNClob(int parameterIndex, NClob value) throws SQLException
/*     */   {
/* 569 */     this.stmt.setNClob(parameterIndex, value);
/*     */   }
/*     */   
/*     */   public void setClob(int parameterIndex, Reader reader, long length)
/*     */     throws SQLException
/*     */   {
/* 575 */     this.stmt.setClob(parameterIndex, reader, length);
/*     */   }
/*     */   
/*     */   public void setBlob(int parameterIndex, InputStream inputStream, long length)
/*     */     throws SQLException
/*     */   {
/* 581 */     this.stmt.setBlob(parameterIndex, inputStream, length);
/*     */   }
/*     */   
/*     */   public void setNClob(int parameterIndex, Reader reader, long length)
/*     */     throws SQLException
/*     */   {
/* 587 */     this.stmt.setNClob(parameterIndex, reader, length);
/*     */   }
/*     */   
/*     */   public void setSQLXML(int parameterIndex, SQLXML xmlObject)
/*     */     throws SQLException
/*     */   {
/* 593 */     this.stmt.setSQLXML(parameterIndex, xmlObject);
/*     */   }
/*     */   
/*     */   public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength)
/*     */     throws SQLException
/*     */   {
/* 599 */     this.stmt.setObject(parameterIndex, x, targetSqlType, scaleOrLength);
/*     */   }
/*     */   
/*     */   public void setAsciiStream(int parameterIndex, InputStream x, long length)
/*     */     throws SQLException
/*     */   {
/* 605 */     this.stmt.setAsciiStream(parameterIndex, x, length);
/*     */   }
/*     */   
/*     */   public void setBinaryStream(int parameterIndex, InputStream x, long length)
/*     */     throws SQLException
/*     */   {
/* 611 */     this.stmt.setBinaryStream(parameterIndex, x, length);
/*     */   }
/*     */   
/*     */   public void setCharacterStream(int parameterIndex, Reader reader, long length)
/*     */     throws SQLException
/*     */   {
/* 617 */     this.stmt.setCharacterStream(parameterIndex, reader, length);
/*     */   }
/*     */   
/*     */   public void setAsciiStream(int parameterIndex, InputStream x)
/*     */     throws SQLException
/*     */   {
/* 623 */     this.stmt.setAsciiStream(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setBinaryStream(int parameterIndex, InputStream x)
/*     */     throws SQLException
/*     */   {
/* 629 */     this.stmt.setBinaryStream(parameterIndex, x);
/*     */   }
/*     */   
/*     */   public void setCharacterStream(int parameterIndex, Reader reader)
/*     */     throws SQLException
/*     */   {
/* 635 */     this.stmt.setCharacterStream(parameterIndex, reader);
/*     */   }
/*     */   
/*     */   public void setNCharacterStream(int parameterIndex, Reader value)
/*     */     throws SQLException
/*     */   {
/* 641 */     this.stmt.setNCharacterStream(parameterIndex, value);
/*     */   }
/*     */   
/*     */   public void setClob(int parameterIndex, Reader reader) throws SQLException
/*     */   {
/* 646 */     this.stmt.setClob(parameterIndex, reader);
/*     */   }
/*     */   
/*     */   public void setBlob(int parameterIndex, InputStream inputStream)
/*     */     throws SQLException
/*     */   {
/* 652 */     this.stmt.setBlob(parameterIndex, inputStream);
/*     */   }
/*     */   
/*     */   public void setNClob(int parameterIndex, Reader reader) throws SQLException
/*     */   {
/* 657 */     this.stmt.setNClob(parameterIndex, reader);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/StatTrackingPreparedStatement.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */